package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.PeriodDetailModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
/**
 * 水期详细
 * @author zhaokf
 * @date 2013-04-23
 *
 */
public class PeriodDetailService extends BaseService
{
	/**
	 * 分页列出水期详细
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<PeriodDetailModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("ped_pe_jfq") != null){
				where = " AND ped_pe_jfq=" + search.get("ped_pe_jfq");
			}
			if (search.get("ped_pe_code") != null){
				where = " AND ped_pe_code LIKE '%" + search.get("ped_pe_code") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<PeriodDetailModel> pd = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SUP_PERIOD_DETAIL " + where;

		try{
			pd = PeriodDetailModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return pd;
	}



	/**
	 * 增加水期详细信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		String SQL = "insert into sup_period_detail(ped_pe_jfq,ped_pe_code,ped_month,ped_period) values(?,?,?,?)";
		int isupadded = Db.update(SQL, params.get("ped_pe_jfq"),params.get("ped_pe_code"),params.get("ped_month"),params.get("ped_period"));
		if(isupadded==1){
			return true;
		}
		return setError("你访问的水期详细信息已存在！");
	}

	/**
	 * 修改水期详细信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String SQL = "update sup_period_detail set ped_pe_jfq=?,ped_pe_code=?,ped_month=?,ped_period=?";
		int isupadded = Db.update(SQL, params.get("ped_pe_jfq"),params.get("ped_pe_code"),params.get("ped_month"),params.get("ped_period"));
		if(isupadded==1){
			return true;
		}
		return setError("您访问的水期详细信息不存在！");
	}

	/**
	 * 删除水期详细信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("ped_pe_code")==null?"":params.get("ped_pe_code").toString();
		String sql = "select * from sup_period_detail where ped_pe_code=?";
		PeriodDetailModel pd = PeriodDetailModel.dao.findFirst(sql, no);
		if (pd != null)
		{
			pd.delete();
			return true;
		}
		return setError("您访问的水期详细不存在！");
	}
	public static PeriodDetailModel get(Map<String, String> params)
	{
		String no = params.get("ped_pe_code")==null?"":params.get("ped_pe_code").toString();
		PeriodDetailModel pdm = PeriodDetailModel.dao.findFirst("SELECT * FROM sup_period_detail WHERE ped_pe_code=?", no);
		return pdm;
	}
}

